# Author: Yubin Lim(purewell@gmail.com)
# Date: 2008-01-18
# License: BSD License
# Copyright: All rights reserved by Yubin Lim.

include(CheckIncludeFile)
include(CheckFunctionExists)
include(CheckTypeSize)

add_definitions(-D_FILE_OFFSET_BITS=64)
add_definitions(-D_LARGEFILE_SOURCE)
add_definitions(-D_LARGE_FILES)
add_definitions(-Wall)
#add_definitions(-g)
add_definitions(-O2)

include_directories("${PROJECT_SOURCE_DIR}/include")

set(CMAKE_EXTRA_INCLUDE_FILES "sys/socket.h")

check_include_file(unistd.h HAVE_UNISTD_H)
check_include_file(fcntl.h HAVE_FCNTL_H)
check_include_file(ioctl.h HAVE_IOCTL_H)
check_include_file("sys/ioctl.h" HAVE_SYS_IOCTL_H)
check_include_file(poll.h HAVE_POLL_H)
check_include_file(dlfcn.h HAVE_DLFCN_H)
check_include_file(semaphore.h HAVE_SEMAPHORE_H)
check_include_file(mqueue.h HAVE_MQUEUE_H)
check_include_file("sys/poll.h" HAVE_SYS_POLL_H)
check_include_file("sys/epoll.h" HAVE_SYS_EPOLL_H)
check_include_file("sys/mman.h" HAVE_SYS_MMAN_H)
check_include_file("sys/stat.h" HAVE_SYS_STAT_H)
check_include_file("stat.h" HAVE_STAT_H)
check_include_file("sys/statvfs.h" HAVE_SYS_STATVFS_H)
check_include_file("statvfs.h" HAVE_STATVFS_H)
check_include_file(inttype.h HAVE_INTTYPE_H)
check_include_file(stdint.h HAVE_STDINT_H)

check_function_exists(epoll_ctl HAVE_EPOLL_CTL)
check_function_exists(poll HAVE_POLL)
check_function_exists(strcasecmp HAVE_STRCASECMP)
check_function_exists(stricmp HAVE_STRICMP)
check_function_exists(vsnprintf HAVE_VSNPRINTF)
check_function_exists(pthread_condattr_setclock
	HAVE_PTHREAD_CONDATTR_SETCLOCK)
check_function_exists(pthread_condattr_getclock
	HAVE_PTHREAD_CONDATTR_GETCLOCK)

check_type_size("void* restrict" HAVE_RESTRICT)
check_type_size("void* __restrict__" HAVE_RESTRICT_CPP)
check_type_size("socklen_t" HAVE_SOCKLEN_T)
check_type_size("clockid_t" HAVE_CLOCKID_T)
check_type_size("size_t" HAVE_SIZE_T)

set(CMAKE_EXTRA_INCLUDE_FILES "sys/stat.h")
check_function_exists(stat HAVE_STAT)
check_function_exists(fstat HAVE_FSTAT)
check_function_exists(lstat HAVE_LSTAT)
check_type_size("struct stat" HAVE_STAT_T)

set(CMAKE_EXTRA_INCLUDE_FILES "sys/stat.h")
check_function_exists(statvfs HAVE_STATVFS)
check_function_exists(fstatvfs HAVE_FSTATVFS)
check_type_size("struct statvfs" HAVE_STATVFS_T)

configure_file(sys_config.h.in "${CMAKE_CURRENT_SOURCE_DIR}/sys_config.h")

file(GLOB SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")

#set(CMAKE_CXX_FLAGS_DEBUG "-O -DDEBUG -g -Wall")
#set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG -Wall")

add_library(purewell SHARED ${SRCS})
set_target_properties(purewell
	PROPERTIES SOVERSION "${PROJECT_VERSION}"
	VERSION "${PROJECT_VERSION}")

add_subdirectory(code)

install(TARGETS purewell
	RUNTIME DESTINATION bin
	LIBRARY DESTINATION ${LIBDIRNAME}
	ARCHIVE DESTINATION ${LIBDIRNAME})
